<?php
if (!defined('ROOT_PATH')) die('No direct access.');
	if (isset($_SESSION['logged'])){
		switch ($param[1]){
			case 'keranjang' :
				view_keranjang();
				break;
			case 'toko' :
				view_toko();
				break;
			case 'transaksi' :
				view_transaksi();
				break;
			case 'alamat' :
				if (isset($_POST['tambah-alamat'])){
					$id = $_POST['index'];
					$nama = amankan($_POST['nama']);
					$kontak = $_POST['kontak'];
					$kota = $_POST['kota'];
					$alamat = amankan($_POST['alamat']);
					$ret = addAlamat($id,$nama,$kontak,$kota,$alamat);
					if ($ret==0){
						//gagal
						$_SESSION['message'] = "Gagal menambahkan alamat";
					}else{
						//sukses
						$_SESSION['message'] = "Sukses menambahkan alamat";
					}
					echo $_SESSION['message'];
					header('Location: ' . $_SERVER['HTTP_REFERER']);
				}else
					view_alamat();
				break;
			case 'edit' :
				if (isset($param[2])){
					switch ($param[2]){
					case 'profil':
						if (isset($_POST['edit-profil'])){
							$id = $_POST['index'];
							$fb = $_POST['idfb'];
							$nama = amankan($_POST['nama']);
							$kontak = $_POST['kontak'];
							$email = $_POST['email'];
							$desc = amankan($_POST['desc']);
							$ret = editProfil($id,$nama,$kontak,$email,$desc);
							if ($ret==0){
								//gagal
								header('Location: ' . $_SERVER['HTTP_REFERER']);	//redirect ke halaman editing
							}else{
								//sukses
								//ubah session
								$_SESSION['logged']['name'] = $nama;
								$_SESSION['logged']['kontak'] = $kontak;
								$_SESSION['logged']['email'] = $email;
								header('Location: ' .PROFIL_URL.'a/'.$fb);	//redirect ke halaman artikelnya
							}
						}else
							edit_profil();
						break;
					case 'alamat':
						if (isset($_POST['edit-alamat'])){
							$id = $_POST['index'];
							$nama = amankan($_POST['nama']);
							$kontak = $_POST['kontak'];
							$kota = $_POST['kota'];
							$alamat = amankan($_POST['alamat']);
							$ret = editAlamat($id,$nama,$kontak,$kota,$alamat);
							if ($ret==0){
								//gagal
								$_SESSION['message'] = "Gagal edit alamat";
								header('Location: ' . $_SERVER['HTTP_REFERER']);
							}else{
								//sukses
								$_SESSION['message'] = "Sukses edit alamat";
								header('Location: ' . PROFIL_URL.'alamat');
							}
						}else
							edit_alamat($param[3]);
						break;
					case 'barang':
						edit_barang($param[3]);
						break;
					default :
						view_404();
						break;
					}
				}else
					view_404();
				break;
			case 'a':
				if (isset($param[3])){
					switch ($param[3]){
					case 'artikel':
						view_artikel($param[2]);
						break;
					case 'toko':
						$toko = getNamaToko($param[2]);
						header('Location: ' . TOKO_URL."a/$param[2]/$toko");
						break;
					default :
						view_profil($param[2]);
						break;
					}
				}else
					view_profil($param[2]);
				break;
			default :
				view_404();
				break;
		}
	}else if ($param[1]=='a'){
		if (isset($param[3])){
			switch ($param[3]){
			case 'artikel':
				view_artikel($param[2]);
				break;
			default :
				view_profil($param[2]);
				break;
			}
		}else
			view_profil($param[2]);
	} else view_404();
?>
